/*
 * $Id: StackBlurFilter.java 4082 2011-11-15 18:39:43Z kschaefe $
 *
 * Dual-licensed under LGPL (Sun and Romain Guy) and BSD (Romain Guy).
 *
 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * Copyright (c) 2006 Romain Guy <romain.guy@mac.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.jdesktop.swingx.image;

import java.awt.image.BufferedImage;

import org.jdesktop.swingx.util.GraphicsUtilities;

/**
 * <p>
 * A stack blur filter can be used to create an approximation of a Gaussian
 * blur. The approximation is controlled by the number of times the
 * {@link org.jdesktop.swingx.image.FastBlurFilter} is applied onto the source
 * picture. The default number of iterations, 3, provides a decent compromise
 * between speed and rendering quality.
 * </p>
 * <p>
 * The force of the blur can be controlled with a radius and the default radius
 * is 3. Since the blur clamps values on the edges of the source picture, you
 * might need to provide a picture with empty borders to avoid artifacts at the
 * edges. The performance of this filter are independent from the radius.
 * </p>
 *
 * @author Romain Guy <romain.guy@mac.com>
 */
public class StackBlurFilter extends AbstractFilter {
	private final int radius;
	private final int iterations;

	/**
	 * <p>
	 * Creates a new blur filter with a default radius of 3 and 3 iterations.
	 * </p>
	 */
	public StackBlurFilter() {
		this(3, 3);
	}

	/**
	 * <p>
	 * Creates a new blur filter with the specified radius and 3 iterations. If
	 * the radius is lower than 1, a radius of 1 will be used automatically.
	 * </p>
	 *
	 * @param radius
	 *            the radius, in pixels, of the blur
	 */
	public StackBlurFilter(int radius) {
		this(radius, 3);
	}

	/**
	 * <p>
	 * Creates a new blur filter with the specified radius. If the radius is
	 * lower than 1, a radius of 1 will be used automatically. The number of
	 * iterations controls the approximation to a Gaussian blur. If the number
	 * of iterations is lower than 1, one iteration will be used automatically.
	 * </p>
	 *
	 * @param radius
	 *            the radius, in pixels, of the blur
	 * @param iterations
	 *            the number of iterations to approximate a Gaussian blur
	 */
	public StackBlurFilter(int radius, int iterations) {
		if (radius < 1) {
			radius = 1;
		}
		if (iterations < 1) {
			iterations = 1;
		}

		this.radius = radius;
		this.iterations = iterations;
	}

	/**
	 * <p>
	 * Returns the effective radius of the stack blur. If the radius of the blur
	 * is 1 and the stack iterations count is 3, then the effective blur radius
	 * is 1 * 3 = 3.
	 * </p>
	 * 
	 * @return the number of iterations times the blur radius
	 */
	public int getEffectiveRadius() {
		return getIterations() * getRadius();
	}

	/**
	 * <p>
	 * Returns the radius used by this filter, in pixels.
	 * </p>
	 *
	 * @return the radius of the blur
	 */
	public int getRadius() {
		return radius;
	}

	/**
	 * <p>
	 * Returns the number of iterations used to approximate a Gaussian blur.
	 * </p>
	 *
	 * @return the number of iterations used by this blur
	 */
	public int getIterations() {
		return iterations;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public BufferedImage filter(BufferedImage src, BufferedImage dst) {
		int width = src.getWidth();
		int height = src.getHeight();

		if (dst == null) {
			dst = createCompatibleDestImage(src, null);
		}

		int[] srcPixels = new int[width * height];
		int[] dstPixels = new int[width * height];

		GraphicsUtilities.getPixels(src, 0, 0, width, height, srcPixels);
		for (int i = 0; i < iterations; i++) {
			// horizontal pass
			FastBlurFilter.blur(srcPixels, dstPixels, width, height, radius);
			// vertical pass
			FastBlurFilter.blur(dstPixels, srcPixels, height, width, radius);
		}
		// the result is now stored in srcPixels due to the 2nd pass
		GraphicsUtilities.setPixels(dst, 0, 0, width, height, srcPixels);

		return dst;
	}
}
